import { http } from './http'

export interface Grade {
  id: number
  name: string
  level: number
}

export interface KnowledgePoint {
  id: number
  name: string
  description: string
  grade_id: number
}

export interface TestConfig {
  grade_id: number
  knowledge_point_ids?: number[]
  difficulty?: number
  question_count: number
  question_types?: string[]
  time_limit?: number
}

export interface TestPaper {
  id: number
  title: string
  description?: string
  total_score: number
  time_limit?: number
  questions: Question[]
}

export interface Question {
  id: number
  content: string
  type: string
  options?: string[]
  score: number
  correct_answer: string
  explanation?: string
}

export interface TestAnswers {
  answers: Record<number, string>
  user_id?: number
}

export interface TestResult {
  total_score: number
  user_score: number
  correct_count: number
  total_questions: number
  questions: QuestionWithAnswer[]
  suggestions?: string[]
}

export interface QuestionWithAnswer extends Question {
  user_answer?: string
  is_correct?: boolean
}

export const mathTestApi = {
  // 获取所有年级
  getGrades: (): Promise<Grade[]> => {
    return http.get('/math-test/grades')
  },

  // 获取指定年级的知识点
  getKnowledgePoints: (gradeId: number): Promise<KnowledgePoint[]> => {
    return http.get(`/math-test/grades/${gradeId}/knowledge-points`)
  },

  // 生成测试试卷
  generateTest: (config: TestConfig): Promise<TestPaper> => {
    return http.post('/math-test/generate-test', config)
  },

  // 提交测试答案
  submitTest: (testPaperId: number, answers: TestAnswers): Promise<TestResult> => {
    return http.post(`/math-test/submit-test/${testPaperId}`, answers)
  },

  // 获取测试试卷详情
  getTestPaper: (testPaperId: number): Promise<TestPaper> => {
    return http.get(`/math-test/test-papers/${testPaperId}`)
  },

  // 获取测试记录
  getTestAttempt: (attemptId: number): Promise<any> => {
    return http.get(`/math-test/test-attempts/${attemptId}`)
  },

  // 历史记录列表（示例接口）
  getHistoryList: (params?: { grade?: number; start?: string; end?: string; page?: number; pageSize?: number; sortBy?: string; sortOrder?: 'asc' | 'desc' }): Promise<{ items: any[]; total: number }> => {
    return http.get('/math-test/history', { params })
  }
}

export default mathTestApi